{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "id": "mvlw9p3tPJjr",
    "colab_type": "code",
    "outputId": "2a4a8f52-315e-42b3-9d49-e9c7c3358979",
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 129.0
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: 1000 cost = 0.147408\n",
      "Epoch: 2000 cost = 0.026562\n",
      "Epoch: 3000 cost = 0.010481\n",
      "Epoch: 4000 cost = 0.005095\n",
      "Epoch: 5000 cost = 0.002696\n",
      "[['i', 'like'], ['i', 'love'], ['i', 'hate']] -> ['dog', 'coffee', 'milk']\n"
     ]
    }
   ],
   "source": [
    "# code by Tae Hwan Jung @graykode\n",
    "import numpy as np\n",
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.optim as optim\n",
    "from torch.autograd import Variable\n",
    "\n",
    "dtype = torch.FloatTensor\n",
    "\n",
    "sentences = [ \"i like dog\", \"i love coffee\", \"i hate milk\"]\n",
    "\n",
    "word_list = \" \".join(sentences).split()\n",
    "word_list = list(set(word_list))\n",
    "word_dict = {w: i for i, w in enumerate(word_list)}\n",
    "number_dict = {i: w for i, w in enumerate(word_list)}\n",
    "n_class = len(word_dict) # number of Vocabulary\n",
    "\n",
    "# NNLM Parameter\n",
    "n_step = 2 # n-1 in paper\n",
    "n_hidden = 2 # h in paper\n",
    "m = 2 # m in paper\n",
    "\n",
    "def make_batch(sentences):\n",
    "    input_batch = []\n",
    "    target_batch = []\n",
    "\n",
    "    for sen in sentences:\n",
    "        word = sen.split()\n",
    "        input = [word_dict[n] for n in word[:-1]]\n",
    "        target = word_dict[word[-1]]\n",
    "\n",
    "        input_batch.append(input)\n",
    "        target_batch.append(target)\n",
    "\n",
    "    return input_batch, target_batch\n",
    "\n",
    "# Model\n",
    "class NNLM(nn.Module):\n",
    "    def __init__(self):\n",
    "        super(NNLM, self).__init__()\n",
    "        self.C = nn.Embedding(n_class, m)\n",
    "        self.H = nn.Parameter(torch.randn(n_step * m, n_hidden).type(dtype))\n",
    "        self.W = nn.Parameter(torch.randn(n_step * m, n_class).type(dtype))\n",
    "        self.d = nn.Parameter(torch.randn(n_hidden).type(dtype))\n",
    "        self.U = nn.Parameter(torch.randn(n_hidden, n_class).type(dtype))\n",
    "        self.b = nn.Parameter(torch.randn(n_class).type(dtype))\n",
    "\n",
    "    def forward(self, X):\n",
    "        X = self.C(X)\n",
    "        X = X.view(-1, n_step * m) # [batch_size, n_step * n_class]\n",
    "        tanh = torch.tanh(self.d + torch.mm(X, self.H)) # [batch_size, n_hidden]\n",
    "        output = self.b + torch.mm(X, self.W) + torch.mm(tanh, self.U) # [batch_size, n_class]\n",
    "        return output\n",
    "\n",
    "model = NNLM()\n",
    "\n",
    "criterion = nn.CrossEntropyLoss()\n",
    "optimizer = optim.Adam(model.parameters(), lr=0.001)\n",
    "\n",
    "input_batch, target_batch = make_batch(sentences)\n",
    "input_batch = Variable(torch.LongTensor(input_batch))\n",
    "target_batch = Variable(torch.LongTensor(target_batch))\n",
    "\n",
    "# Training\n",
    "for epoch in range(5000):\n",
    "\n",
    "    optimizer.zero_grad()\n",
    "    output = model(input_batch)\n",
    "\n",
    "    # output : [batch_size, n_class], target_batch : [batch_size] (LongTensor, not one-hot)\n",
    "    loss = criterion(output, target_batch)\n",
    "    if (epoch + 1)%1000 == 0:\n",
    "        print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.6f}'.format(loss))\n",
    "\n",
    "    loss.backward()\n",
    "    optimizer.step()\n",
    "\n",
    "# Predict\n",
    "predict = model(input_batch).data.max(1, keepdim=True)[1]\n",
    "\n",
    "# Test\n",
    "print([sen.split()[:2] for sen in sentences], '->', [number_dict[n.item()] for n in predict.squeeze()])\n"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "name": "NNLM-Torch.ipynb",
   "version": "0.3.2",
   "provenance": [],
   "collapsed_sections": []
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  },
  "accelerator": "GPU"
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
